#include "stdafx.h"
#include "IniFile.h"
#include <string>

using namespace std;


CIniFile::CIniFile()
{
}

void CIniFile::SetFilePath(CString fileName)
{
	mFilePath = GetCurrentPath()+fileName;
}

CIniFile::~CIniFile()
{
}

int CIniFile::GetSectionValueInt(CString &strSection, CString &strName)
{
	int nCount = GetPrivateProfileInt(strSection.GetBuffer(0), strName.GetBuffer(0), 0, mFilePath);
	return nCount;
}

CString CIniFile::GetSectionValueStr(CString strSection, CString strName)
{
	CString strSectionValue;
	GetPrivateProfileString(strSection, strName, "default", strSectionValue.GetBuffer(MAX_PATH), MAX_PATH, mFilePath);
	strSectionValue.TrimLeft();
	strSectionValue.TrimRight();

	char *pTemp;
	pTemp=strSectionValue.GetBuffer(strSectionValue.GetLength()); 
	strSectionValue.ReleaseBuffer();
	strSectionValue = CString(pTemp);
	return strSectionValue;
}

BOOL CIniFile::SetSectionValueStr(CString &strSection, LPCTSTR strValue, CString &strName)
{
	BOOL bRect = WritePrivateProfileString(strSection.GetBuffer(0), strName.GetBuffer(0), strValue, mFilePath);
	return bRect;
}

CString CIniFile::GetCurrentPath()
{
	char temp[MAX_PATH] = {0};
	string strRet;
	int Pos = 0;
	::GetModuleFileName(NULL,temp,MAX_PATH);
	strRet.append(temp);
	Pos = (int)strRet.rfind("\\");
	strRet = strRet.substr(0,Pos);
	strRet.append("\\");
	return CString(strRet.c_str());
}